package com.example.springbott.wx_service;

import com.example.springbott.bean.MarketStorage;
import com.example.springbott.mapper.MarketStorageMapper;
import com.example.springbott.util.OSSUploadUntil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.UUID;

/**
 * 上传图片
 *
 * @author silic xia
 * @since 2022/11/22 14:11
 */
@Service
public class WXStorageServiceImpl implements WXStorageService {

    @Autowired
    MarketStorageMapper storageMapper;


    /**
     *  上传图片--方法一:存储到本地
     *
     *  wx/storage/upload
     * @param /null
     * @return  addTime: "2022-11-22 12:27:26"
     *           id: 293
     *           key: "5gyvforlewb6dunog5hx.jpeg"
     *           name: "fEAh0UnVSkCU513f7389639759b720da663fb0da6841.jpeg"
     *           size: 608106
     *           type: "image/jpeg"
     *           updateTime: "2022-11-22 12:27:26"
     *           url: "http://182.92.235.201:8083/wx/storage/fetch/5gyvforlewb6dunog5hx.jpeg"
     *        data:{}
     *        errmsg: "æå"
     *        errno: 0
     * @author silic xia
     * @since 2022/11/22 12:28
     */
    // @SneakyThrows
    // @Override
    // public MarketStorage wxStorageUpload(HttpServletRequest req, MultipartFile file) {
    //     File storageAddress = new File("C:/spring", file.getOriginalFilename());
    //     file.transferTo(storageAddress);
    //     String[] split = file.getOriginalFilename().split("\\.");
    //     String key = UUID.randomUUID().toString() + "." + split[1];
    //
    //     MarketStorage marketStorage = new MarketStorage();
    //     marketStorage.setKey(key);
    //     marketStorage.setType(file.getContentType());
    //     marketStorage.setAddTime(new Date());
    //     marketStorage.setUpdateTime(new Date());
    //     marketStorage.setName(file.getOriginalFilename());
    //     marketStorage.setSize((int)file.getSize());
    //     marketStorage.setUrl("http://localhost:8083/wx/storage/fetch" + file.getOriginalFilename());
    //     marketStorage.setDeleted(false);
    //     storageMapper.insert(marketStorage);
    //     return marketStorage;
    // }

    /**
     *  上传图片--方法二：OSS对象存储
     *
     *  wx/storage/upload
     * @param /null
     * @return  addTime: "2022-11-22 12:27:26"
     *           id: 293
     *           key: "5gyvforlewb6dunog5hx.jpeg"
     *           name: "fEAh0UnVSkCU513f7389639759b720da663fb0da6841.jpeg"
     *           size: 608106
     *           type: "image/jpeg"
     *           updateTime: "2022-11-22 12:27:26"
     *           url: "http://182.92.235.201:8083/wx/storage/fetch/5gyvforlewb6dunog5hx.jpeg"
     *        data:{}
     *        errmsg: "æå"
     *        errno: 0
     * @author silic xia
     * @since 2022/11/22 12:28
     */
    @SneakyThrows
    @Override
    public MarketStorage wxStorageUpload(HttpServletRequest req, MultipartFile file) {
        String picUrl = OSSUploadUntil.upload(file);

        String[] split = file.getOriginalFilename().split("\\.");
        String key = UUID.randomUUID().toString() .replaceAll("-","") + "." + split[1];

        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(key);
        marketStorage.setType(file.getContentType());
        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(new Date());
        marketStorage.setName(file.getOriginalFilename());
        marketStorage.setSize((int)file.getSize());
        marketStorage.setUrl(picUrl);
        marketStorage.setDeleted(false);
        storageMapper.insert(marketStorage);
        return marketStorage;
    }
}

